/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.sober.transaction;



import com.jspx.sober.SoberEnv;
import com.jspx.sober.SoberSupport;
import com.jspx.sober.util.JdbcUtil;
import com.jspx.sober.exception.TransactionException;
import java.sql.Savepoint;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2008-8-5
 * Time: 16:09:48
 * AbstractTransaction   extends
 */
public class JDBCTransaction extends AbstractTransaction
{
     public JDBCTransaction(SoberSupport soberSupport)
    {
        this.soberSupport = soberSupport;
    }

    private JDBCTransaction()
    {

    }


    private Savepoint savepoint = null;


    /**
     * JDBC事务开始,简单的设置 AutoCommit(false);
     * @throws TransactionException
     */
    public void begin() throws TransactionException
    {
        isActive++;
        if (isActive==1)
        try
        {
            if (connection.getAutoCommit())
                connection.setAutoCommit(false);
            if (supportsSavePoints) savepoint = connection.setSavepoint();
        } catch (SQLException e)
        {
            try {
                connection.releaseSavepoint(savepoint);
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            savepoint = null;
            throw new TransactionException(e,"Error Transaction begin not to get savepoint,jdbc事务不能够开启,可能是因为不能够得到连接");
        }
    }

    /**
     * 提交,如果存在事务嵌套,那么以最外边的事务为准
     * @throws TransactionException
     */
    public void commit() throws TransactionException
    {
        isActive--;
        if (isActive > 0) return;
        if (wasCommitted  || wasRolledBack) return;
        try
        {
            if (connection==null||connection.isClosed()) return;
            if (!connection.getAutoCommit()) connection.commit();
        } catch (SQLException e)
        {
            throw new TransactionException(e,"Error Transaction commit ,jdbc事务不能够提交,可能是因为不能够得到连接或是没有开始事务就提交了");
        } finally {
            transactionManager.remove(soberSupport.getTid());
            soberSupport.setTid(SoberEnv.notTransaction);
            isActive = 0;
            wasCommitted = true;
            JdbcUtil.closeConnection(connection,false);
        }
    }

    /**
     * 回滚,如果存在事务嵌套,回滚后下边的嵌套将不既有意义
     * @throws TransactionException
     */
    public void rollback() throws TransactionException
    {
        if (wasRolledBack) return;
        isActive--;
        try
        {
            if (connection != null)
            {
                if (supportsSavePoints && savepoint != null)
                    connection.rollback(savepoint);
                else connection.rollback();
            }
        } catch (Exception e)
        {
            throw new TransactionException(e,"Error Transaction rollback ,jdbc事务回滚错误,可能是因为不能够得到连接");
        } finally {
            soberSupport.setTid(SoberEnv.notTransaction);
            transactionManager.remove(soberSupport.getTid());
            isActive = 0;
            wasRolledBack = true;
            JdbcUtil.closeConnection(connection,true);
        }
    }
    
  
}